package cn.bs.busuo.hotel.controller;

import cn.bs.busuo.hotel.pojo.param.*;
import cn.bs.busuo.hotel.pojo.vo.HotelRoomTypeEnabledVO;
import cn.bs.busuo.hotel.pojo.vo.HotelVO;
import cn.bs.busuo.hotel.pojo.vo.HotelWorkerVO;
import cn.bs.busuo.hotel.pojo.vo.HotelsRoomTypeCheckVO;
import cn.bs.busuo.hotel.service.HotelService;
import cn.bs.busuo.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/hotels")
@Api(tags = "3.管理员酒店管理")
public class HotelController {
    // TODO 设置security权限限制(最后验证)  添加新酒店时添加该酒店到未设置景点和未设置城市的两个表(完成,景点还没有确认)  景点也是
    // TODO 系统管理员添加删除资讯 酒店 景点 商城管理员的接口(完成,检查别人的代码是否有这种权限) 添加删除查询收货地址  数据库的表改过(添加了景点和酒店的价格字段),记得重新建表改sql
    // TODO review所有人的代码并测试 添加判断,如添加房型时酒店是否存在并启用,是否有相同名字的房型,减少房间时判断是否有足够的房间,增加房间时判断是否有对应的房型
    // TODO 收货地址表的接口不知道写没写 用户退出登录时删除用户的相关缓存信息
    @Autowired
    private HotelService hotelService;

    /**
     * 添加酒店的接口
     */
    @PostMapping("/add-new")
    @ApiOperation("添加酒店")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult addNew(@Valid AddNewHotelParam addNewHotelParam) {
        hotelService.addNew(addNewHotelParam);
        return JsonResult.ok();
    }

    /**
     * 删除酒店的接口(启用状态改为0)
     */
    @GetMapping("/delete")
    @ApiOperation("删除酒店")
    @ApiOperationSupport(order = 110)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult delete(Long hotelId) {
        hotelService.delete(hotelId);
        return JsonResult.ok();
    }

    /**
     * 酒店修改信息的接口
     */
    @PostMapping("/update")
    @ApiOperation("修改酒店信息")
    @ApiOperationSupport(order = 130)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult update(@Valid UpdateHotelInformationParam updateHotelInformationParam) {
        hotelService.update(updateHotelInformationParam);
        return JsonResult.ok();
    }

    /**
     * 设置景点附近的酒店
     */
    @PostMapping("/set-position-about-sight")
    @ApiOperation("设置景点附近的酒店")
    @ApiOperationSupport(order = 140)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult setPositionAboutSight(@Valid SetHotelPositionAboutSightParam setHotelPositionAboutSightParam) {
        hotelService.setPositionAboutSight(setHotelPositionAboutSightParam);
        return JsonResult.ok();
    }

    /**
     * 设置城市附近的酒店
     */
    @PostMapping("/set-position-about-city")
    @ApiOperation("设置城市附近的酒店")
    @ApiOperationSupport(order = 150)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult setPositionAboutCity(@Valid SetHotelPositionAboutCityParam setHotelPositionAboutCityParam) {
        hotelService.setPositionAboutCity(setHotelPositionAboutCityParam);
        return JsonResult.ok();
    }

    /**
     * 查询所有酒店(enable为1)
     */
    @GetMapping("/select-all")
    @ApiOperation("查询所有酒店")
    @ApiOperationSupport(order = 160)
    public JsonResult selectAll() {
        List<HotelVO> hotelVOS= hotelService.selectAll();
        return JsonResult.ok(hotelVOS);
    }

    /**
     * 根据id查询酒店(enable为1)
     */
    @GetMapping("/{hotelId:[0-9]+}/select-by-id")
    @ApiOperation("根据id查询酒店")
    @ApiOperationSupport(order = 170)
    public JsonResult selectById(@PathVariable @Range(min = 1, message = "请提交合法的酒店ID值！") Long hotelId) {
        HotelVO hotelVO= hotelService.selectById(hotelId);
        return JsonResult.ok(hotelVO);
    }

    /**
     * 根据城市id查询酒店(enable为1)
     */
    @GetMapping("/{cityId:[0-9]+}/select-hotel-by-city-id")
    @ApiOperation("根据城市id查询酒店")
    @ApiOperationSupport(order = 180)
    public JsonResult selectHotelByCityId(@PathVariable @Range(min = 1, message = "请提交合法的城市ID值！")Long cityId) {
        List<HotelVO> hotelVOS= hotelService.selectHotelByCityId(cityId);
        return JsonResult.ok(hotelVOS);
    }

    /**
     * 根据景点id查询附近酒店(enable为1)
     */
    @GetMapping("/{sightId:[0-9]+}/select-hotel-by-sight-id")
    @ApiOperation("根据景点id查询附近酒店")
    @ApiOperationSupport(order = 190)
    public JsonResult selectHotelBySightId(@PathVariable @Range(min = 1, message = "请提交合法的景点ID值！")Long sightId) {
        List<HotelVO> hotelVOS= hotelService.selectHotelBySightId(sightId);
        return JsonResult.ok(hotelVOS);
    }

    /**
     * 查询待设置城市的酒店(enable为1)
     */
    @GetMapping("/select-hotel-which-not-set-city")
    @ApiOperation("查询待设置城市的酒店")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult selectCityIfExist() {
        List<HotelVO> hotelVOS = hotelService.selectHotelWhichNotSetCity();
        return JsonResult.ok(hotelVOS);
    }

    /**
     * 查询待设置景点的酒店(enable为1)
     */
    @GetMapping("/select-hotel-which-not-set-sight")
    @ApiOperation("查询待设置景点的酒店")
    @ApiOperationSupport(order = 210)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult selectSightIfExist() {
        List<HotelVO> hotelVOS = hotelService.selectHotelWhichNotSetSight();
        return JsonResult.ok(hotelVOS);
    }

    /**
     * 添加酒店工作人员(同时添加酒店管理人表)
     */
    @PostMapping("/add-hotel-worker")
    @ApiOperation("添加酒店工作人员")
    @ApiOperationSupport(order = 220)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult addHotelWorker(@Valid AddNewHotelWorkerParam addNewHotelWorkerParam) {
        hotelService.addHotelWorker(addNewHotelWorkerParam);
        return JsonResult.ok();
    }

    /**
     * 删除酒店工作人员(同时删除酒店管理人表)
     */
    @GetMapping("/delete-hotel-worker")
    @ApiOperation("删除酒店工作人员")
    @ApiOperationSupport(order = 230)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult deleteHotelWorker(@Valid DeleteHotelWorkerParam deleteHotelWorkerParam) {
        hotelService.deleteHotelWorker(deleteHotelWorkerParam);
        return JsonResult.ok();
    }

    /**
     * 查询酒店工作人员
     */
    @GetMapping("/{hotelId:[0-9]+}/select-hotel-worker")
    @ApiOperation("查询酒店工作人员")
    @ApiOperationSupport(order = 240)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult selectHotelWorker(@PathVariable @Range(min = 1, message = "请提交合法的酒店ID值！") Long hotelId) {
        List<HotelWorkerVO> hotelWorkerVOS = hotelService.selectHotelWorker(hotelId);
        return JsonResult.ok(hotelWorkerVOS);
    }

    /**
     * 添加房型
     */
    @PostMapping("/add-room-type")
    @ApiOperation("添加房型")
    @ApiOperationSupport(order = 250)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult addRoomType(@Valid AddNewRoomTypeParam addNewRoomTypeParam) {
        hotelService.addRoomType(addNewRoomTypeParam);
        return JsonResult.ok();
    }

    /**
     * 删除房型(启用状态改为0)
     */
    @GetMapping("/delete-room-type")
    @ApiOperation("删除房型")
    @ApiOperationSupport(order = 260)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult deleteRoomType(@Valid DeleteHotelRoomTypeParam deleteHotelRoomTypeParam) {
        hotelService.deleteRoomType(deleteHotelRoomTypeParam);
        return JsonResult.ok();
    }

    /**
     * 查询未审核的房型
     */
    @GetMapping("/select-room-type-which-not-check")
    @ApiOperation("查询未审核的房型")
    @ApiOperationSupport(order = 270)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult selectRoomTypeWhichNotCheck() {
        List<HotelsRoomTypeCheckVO> roomTypeVOS = hotelService.selectRoomTypeWhichNotCheck();
        return JsonResult.ok(roomTypeVOS);
    }

    /**
     * 查询审核过的房型
     */
    @GetMapping("/select-room-type-which-check")
    @ApiOperation("查询审核过的房型")
    @ApiOperationSupport(order = 280)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult selectRoomTypeWhichCheck() {
        List<HotelsRoomTypeCheckVO> roomTypeVOS = hotelService.selectRoomTypeWhichCheck();
        return JsonResult.ok(roomTypeVOS);
    }
    /**
     * 审核通过房型
     */
    @PostMapping("/check-room-type")
    @ApiOperation("审核通过房型")
    @ApiOperationSupport(order = 290)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult checkRoomTypeAccess(@Valid CheckRoomTypeParam checkRoomTypeParam) {
        hotelService.checkRoomTypeAccess(checkRoomTypeParam.getId());
        return JsonResult.ok();
    }

    /**
     * 审核拒绝房型
     */
    @PostMapping("/refuse-room-type")
    @ApiOperation("审核拒绝房型")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult refuseRoomTypeAccess(@Valid CheckRoomTypeParam checkRoomTypeParam) {
        hotelService.checkRoomTypeRefused(checkRoomTypeParam.getId());
        return JsonResult.ok();
    }

    /**
     * 查询酒店的房型列表(包括房型的房间总数和可用数,enable为1)
     */
    @GetMapping("/{hotelId:[0-9]+}/select-hotel-room-type")
    @ApiOperation("查询酒店的房型列表")
    @ApiOperationSupport(order = 310)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult selectHotelRoomType(@PathVariable @Range(min = 1, message = "请提交合法的酒店ID值！") Long hotelId) {
        List<HotelRoomTypeEnabledVO> hotelRoomTypeVOS = hotelService.selectHotelRoomType(hotelId);
        return JsonResult.ok(hotelRoomTypeVOS);
    }

    /**
     * 启用房型
     */
    @GetMapping("/{id:[0-9]+}/enable-room-type")
    @ApiOperation("启用房型")
    @ApiOperationSupport(order = 320)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult enableRoomType(@PathVariable @Range(min = 1, message = "请提交合法的房型ID值！") Long id) {
        hotelService.enableRoomType(id);
        return JsonResult.ok();
    }

    /**
     * 禁用房型
     */
    @GetMapping("/{id:[0-9]+}/disable-room-type")
    @ApiOperation("禁用房型")
    @ApiOperationSupport(order = 330)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult disableRoomType(@PathVariable @Range(min = 1, message = "请提交合法的房型ID值！") Long id) {
        hotelService.disableRoomType(id);
        return JsonResult.ok();
    }

    /**
     * 添加房间(增加房间总数)
     */
    @PostMapping("/add-room")
    @ApiOperation("添加房间")
    @ApiOperationSupport(order = 340)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult addRoom(@Valid AddNewRoomParam addNewRoomParam) {
        hotelService.addRoom(addNewRoomParam);
        return JsonResult.ok();
    }

    /**
     * 删除房间(减少房间总数)
     */
    @GetMapping("/delete-room")
    @ApiOperation("减少房间")
    @ApiOperationSupport(order = 350)
    @PreAuthorize("hasAuthority('/account/user/hotelsAdmin')")
    public JsonResult deleteRoom(@Valid DeleteRoomParam deleteRoomParam) {
        hotelService.deleteRoom(deleteRoomParam);
        return JsonResult.ok();
    }



}
